const path = require('path');
const fs = require('fs');
const webpack = require('webpack');

const OutputDir = path.join(__dirname, 'dist');
// some preparations
// make sure dirs exist
if (!fs.existsSync(OutputDir)) {
    fs.mkdirSync(OutputDir);
}

module.exports = (env, argv) => {

    let mode = argv.mode === 'production' ? 'production' : 'development';
    const productionMode = mode == 'production';

    return {
        mode,
        context: __dirname,
        node: {
            __filename: true,
            path: false,
        },
        devtool: productionMode ? 'none' : 'cheap-module-eval-source-map',
        entry: {
            'geoland-formatter': './src/geojson-formatter.js',
            main: './src/index.js',
        },
        output: {
            filename: '[name].js',
            path: OutputDir
        },
        module: {
            noParse: /browserfs\.js/,
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader'
                    }
                }
            ]
        },
        devServer: {
            contentBase: [OutputDir, './public'],
            watchContentBase: true,
            host: '0.0.0.0',
            port: '8080',
            clientLogLevel: 'warning',
            disableHostCheck: true,
            overlay: {
                warnings: true,
                errors: true
            },
            hot: true
        },
        stats: {
            timings: true
        },
        plugins: productionMode ? [] : [
            new webpack.NamedModulesPlugin(),
            new webpack.HotModuleReplacementPlugin()
        ]
    };
};
